package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.ExceptionMapper;
import com.kgc.community.mapper.UserMapper;
import com.kgc.community.pojo.Exception;
import com.kgc.community.pojo.ExceptionExample;
import com.kgc.community.pojo.User;
import com.kgc.community.pojo.UserExample;
import com.kgc.community.service.ExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ExceptionServiceImpl implements ExceptionService {

    @Autowired
    ExceptionMapper exceptionMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public PageInfo<Exception> searchExceptionByCondition(Map<Integer,Object> map, Integer pageNum, Integer pageSize) {

        ExceptionExample example = new ExceptionExample();

        if (map.get(1)!=null){

            String name = (String) map.get(1);
            UserExample userExample = new UserExample();
            userExample.createCriteria().andNameLike("%"+name+"%");

            List<User> users = userMapper.selectByExample(userExample);

            List<Integer> idList = new ArrayList<>();
            for (User user : users) {
                idList.add(user.getId());
            }
            idList.add(-1);

            example.createCriteria().andSponsorIdIn(idList);
        }
        if (map.get(2)!=null){
            String name = (String) map.get(1);
            UserExample userExample = new UserExample();
            userExample.createCriteria().andNameLike("%"+name+"%");

            List<User> users = userMapper.selectByExample(userExample);

            List<Integer> idList = new ArrayList<>();
            for (User user : users) {
                idList.add(user.getId());
            }
            idList.add(-1);

            example.createCriteria().andHandleIdIn(idList);
        }
        if (map.get(3)!=null){
            String name = (String) map.get(1);
            UserExample userExample = new UserExample();
            userExample.createCriteria().andNameLike("%"+name+"%");

            List<User> users = userMapper.selectByExample(userExample);

            List<Integer> idList = new ArrayList<>();
            for (User user : users) {
                idList.add(user.getId());
            }
            idList.add(-1);

            example.createCriteria().andLaborerIdIn(idList);
        }
        if (map.get(4)!=null) {
            Integer status = (Integer) map.get(4);

            example.createCriteria().andStatusEqualTo(status);
        }

        if (map.get(5)!=null) {
            Integer type = (Integer) map.get(5);

            example.createCriteria().andTypeEqualTo(type);
        }

        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy("id desc");
        List<Exception> exceptions = exceptionMapper.selectByExample(example);
        if (exceptions!=null && exceptions.size()>0){
            System.out.println(exceptions.get(0).getDescribes());
        }
        PageInfo<Exception> pageInfo = new PageInfo<>(exceptions);

        return pageInfo;
    }

    @Override
    public Integer updateSelectiveById(Exception exception) {
        return exceptionMapper.updateByPrimaryKeySelective(exception);
    }

    @Override
    public Integer insertSelective(Exception exception) {
        return exceptionMapper.insertSelective(exception);
    }

    @Override
    public Integer deleteById(Integer id) {
        return exceptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteByIdList(List<Integer> ids) {
        ExceptionExample example = new ExceptionExample();
        example.createCriteria().andIdIn(ids);

        return exceptionMapper.deleteByExample(example);
    }

    @Override
    public Exception searchById(Integer id) {

        if (id!=null){
            Exception exception = exceptionMapper.selectByPrimaryKey(id);
            return exception;
        }

        return null;
    }
}
